package com.chima.action;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.domain.Post;
import com.chima.domain.Professional;
import com.chima.service.PostService;
import com.chima.service.ProfessionalService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import flexjson.JSONSerializer;

public class ProfessionalAction extends ActionSupport implements ModelDriven<Professional> {
 Professional professional =new Professional();
 
	public Professional getModel() {
		return professional;
	}
	private int start;// 开始页码
	private int limit; //每页显示
	private int page;//页数
	private int param;
	private int depart;
	
	public int getDepart() {
		return depart;
	}

	public void setDepart(int depart) {
		this.depart = depart;
	}

	public int getParam() {
		return param;
	}

	public void setParam(int param) {
		this.param = param;
	}
	private boolean success=false ;
	private ProfessionalService professionalService;
	private List<Professional> professionals;
	
	public ProfessionalService getProfessionalService() {
		return professionalService;
	}

	public void setProfessionalService(ProfessionalService professionalService) {
		this.professionalService = professionalService;
	}

	public List<Professional> getProfessionals() {
		return professionals;
	}

	public void setProfessionals(List<Professional> professionals) {
		this.professionals = professionals;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String  ListProfessional() throws Exception{
	    PageRequestBean pageRequestBean = new PageRequestBean();
	    DetachedCriteria criteria = DetachedCriteria.forClass(Professional.class);
		if(param!=0){ 
			criteria.add(Restrictions.eq("department.id",param));
		}
		if(depart!=0){ 
			criteria.add(Restrictions.eq("department.id",depart));
		}
		pageRequestBean.setCriteria(criteria);
		pageRequestBean.setStart(start);
		pageRequestBean.setLimit(limit);
		pageRequestBean.setPage(page);
		pageRequestBean.setDept(depart);
		PageResponseBean pageResponseBean = professionalService.findProfessional(pageRequestBean);
		professionals=pageResponseBean.getRows();
        int total=0;
        total =pageResponseBean.getTotal();
		// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.include("*.id","*.name");
        jsonSerializer.exclude("*.class");
        String resultJson1 = jsonSerializer.deepSerialize(professionals);  
        jsonSerializer.include("*.id","*.name","*.department","*.grade");
        jsonSerializer.exclude("*.class","*.jobDetails","*.Record","*.Job","*.employees","*.weights","*.Professional","*.DetailPost","*.Post");
//        jsonSerializer.exclude("*.class","*.employees","*.Post","*.DetailPost","*.Post.employees","*.jobDetails","*.records","*.jobs","jobDetail");
        String resultJson2 =jsonSerializer.serialize(total);
        String jsonString = "{total:"+resultJson2.toString()+",professionals:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
  }
	public void addProfessional() throws Exception{
		try {
			professionalService.saveProfessional(professional);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		System.out.println(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
	}
	
	public void editProfessional() throws Exception{
	professionalService.updateProfessional(professional);
	this.success=true;
	JSONSerializer jsonSerializer = new JSONSerializer();
	String resultJson =jsonSerializer.serialize(success);
	String jsonString = "{success:"+resultJson.toString()+"}";
	System.out.println(jsonString);
	ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
	ServletActionContext.getResponse().getWriter().print(jsonString);
}
	public String deleteProfessional(){
			professionalService.deleteProfessional(professional.getId());
		return NONE;
	}

	public String  ListProfessionalBydepart() throws Exception{
	  
		professionals= professionalService.findByDepart(param);
	    int total =professionals.size();;
		// 输出json内容到客户端
	
	    JSONSerializer jsonSerializer = new JSONSerializer();
	    jsonSerializer.include("*.id","*.name","*.department","*.grade");
	    jsonSerializer.exclude("*.class");
	    String resultJson1 = jsonSerializer.deepSerialize(professionals);  
	  
	    String resultJson2 =jsonSerializer.serialize(total);
	    String jsonString = "{total:"+resultJson2.toString()+",professionals:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	}
	
	public String  ListAllProfessional() throws Exception{
		  
		professionals= professionalService.ListAllProfessional();
	    int total =professionals.size();;
		// 输出json内容到客户端
	
	    JSONSerializer jsonSerializer = new JSONSerializer();
	    jsonSerializer.include("*.id","*.name","*.department","*.grade");
	    jsonSerializer.exclude("*.class");
	    String resultJson1 = jsonSerializer.deepSerialize(professionals);  
	    String resultJson2 =jsonSerializer.serialize(total);
	    String jsonString = "{total:"+resultJson2.toString()+",professionals:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	}
}
